﻿@model IOA.MES.DataAccess.VModel.VInboundInspect

@{
    Layout = "~/Views/Shared/_LayoutPage.cshtml";
}

@section Contents{
    <style>
        #txtBarcode {
            width: 360px;
        }

        .layui-footer {
            padding: 10px;
            text-align: center;
        }
    </style>
}

<div id="page"></div>

<script type="text/template" id="tplPage">
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-form-item" style="padding:10px 15px 0 15px;margin-bottom:0;">
                <div class="layui-inline">{{lang.Field.BarCode}}：</div>
                <div class="layui-inline">
                    <input type="text" class="layui-input" id="txtBarCode" placeholder="请输入扫描组件条码" />
                </div>
            </div>

            <div class="layui-card-body">
                <table class="layui-table" id="dataTable" lay-filter="table"></table>
            </div>

            <div class="layui-form-item" style="margin-left:15px;">
                <div class="layui-inline">{{lang.Field.RefuseReason}}：</div>
                <div class="layui-inline">
                    <input id="txtRefuseReason" type="text" class="layui-input" style="width:360px;" placeholder="请填写拒绝原因" value="@(Model == null ? "" : Model.RefuseReason)" />
                </div>
            </div>
            <div class="layui-footer">
                <button id="btnRefuse" class="layui-btn" style="background-color:rgba(255, 153, 102, 1);">{{lang.Field.RefuseInbound}}</button>
                <button id="btnAgree" class="layui-btn layui-btn-normal">{{lang.Field.SubmitInbound}}</button>
            </div>
        </div>
    </div>
</script>

@section Scripts{
    <script>
        var inspectId = getParameter('inspectId');
        var tableIns;
        var options;

        var vm = new Vue({
            el: "#page",
            template: "#tplPage",
            data: {
                lang: JSON.parse(lang).package
            }
        });

        setTimeout(function () {
            initLayui();
        }, 50);
        function initLayui() {
            layui.use(['form', 'table'], function () {
                var table = layui.table;
                options = {
                    elem: '#dataTable',
                    url: '/WorkOrder/QueryInspectProducts',
                    where: {
                        inspectId: inspectId
                    },
                    height: 'full-270',
                    method: 'post',
                    page: false,
                    response: {
                        statusName: 'ErrCode'
                        , statusCode: 200
                        , msgName: 'ErrCode'
                        , countName: 'TotalCount'
                        , dataName: 'Data'
                    },
                    cols: [[
                        { type: 'numbers', title: '序号' },
                        { field: 'BarCode', title: '组件条码', width: 300, align: 'center' },
                        {
                            field: 'Status', title: '状态', width: 120, align: 'center', templet: function (d) {
                                var status = '';
                                switch (d.Status) {
                                    case 0:
                                        status = '未检验';

                                        break;
                                    case 1:
                                        status = '<span style="color:green;">已检验</span>';

                                        break;
                                    case 2:
                                        status = '<span style="color:red;">异常组件</span>';

                                        break;
                                }

                                return status;
                            }
                        }
                    ]]
                };

                tableIns = table.render(options);
            });
        }
        $(document).on("keydown", '#txtBarCode', function (e) {
            if (e.keyCode == 13) {
                var barCode = $('#txtBarCode').val();
                var data = layui.table.cache.dataTable;

                var index = data.findIndex(_ => _.BarCode == barCode);
                if (index >= 0) {
                    data[index].Status = 1;
                } else {
                    data.push({ BarCode: barCode, Status: 2 });
                }

                options.url = '';
                options.data = data;
                tableIns.reload(options);

                $('#txtBarCode').val("");
                $('#txtBarCode').focus();
            }
        });

        $(document).on("click", "#btnRefuse,#btnAgree", function () {
            var elemId = this.getAttribute('id');
            var reason = $('#txtRefuseReason').val().trim();
            var status = 1;
            if (elemId == 'btnRefuse') {
                status = 2;
                if (!reason) {
                    return layer.msg('请填写拒绝理由', { icon: 5 });
                }
            }

            var products = [];
            $.each(layui.table.cache.dataTable, function (i, d) {
                products.push({
                    InspectId: inspectId,
                    BarCode: d.BarCode,
                    Status: d.Status
                });
            });
            if (products.length == 0) {
                return layer.msg('暂无检核产品条码', { icon: 5 });
            }

            $.ajax({
                url: '/WorkOrder/SaveVerification',
                data: {
                    inspect: {
                        PkId: inspectId,
                        RefuseReason: reason,
                        Status: status
                    },
                    products: products
                },
                dataType: 'json',
                success: function (res) {
                    if (res.ErrCode != 200) {
                        return layer.msg(res.ErrMsg, { icon: 5 });
                    }

                    layer.msg('提交成功', { icon: 6, time: 1000 }, function () {
                        parent.layer.close(parent.layer.getFrameIndex(window.name));
                    });
                    parent.$("#btnSearch").trigger('click');
                }
            });
        });
    </script>
}
